﻿using UnityEngine;
using System.Collections;

public class platformScript : activatableObjects {
	
	public bool xAxisMove;
	public Vector3[] nodes = {Vector3.zero, Vector3.zero};
	public float moveSpeed;
	
	int index;
	int direction;
	public bool isBothWorld;
	
	// Use this for initialization
	void Start () {
		float tmpMov;
		
		if(xAxisMove)
			tmpMov =(transform.position - nodes[index]).x; 
		else
			tmpMov =(transform.position - nodes[index]).y; 

		if(tmpMov < 0)
			direction = 1;
		else
			direction = -1;
		
		//this.gameObject
		if(isBothWorld)
			staticTools.SetLayerRecursively(this.gameObject, 0);
	}
	
	public void setInfo(float _from, float _to, bool isXAxis)
	{
		xAxisMove = isXAxis;
		
		//From
		nodes[0].x = transform.position.x + _from;
		nodes[0].y = transform.position.y + _from;
		
		//To
		nodes[1].x = transform.position.x + _to;
		nodes[1].y = transform.position.y + _to;
	}
	
	// Update is called once per frame
	void FixedUpdate () 
	{
		if(isMoving)
		{
			Vector3 tmpPos = transform.position;
			if(xAxisMove)
				tmpPos.x += direction * moveSpeed * Time.deltaTime;
			else
				tmpPos.y += direction * moveSpeed * Time.deltaTime;
			transform.position = tmpPos;
			
			int tmpD;
			float tmpMov;
			
			if(xAxisMove)
				tmpMov =(transform.position - nodes[index]).x; 
			else
				tmpMov =(transform.position - nodes[index]).y; 

			if(tmpMov < 0)
				tmpD = 1;
			else
				tmpD = -1;
			
			
			if(tmpD != direction)
			{
				index ++;
				index %= 2;
				direction *= -1;
			}
		}
	}
	/*
	void OnCollisionEnter(Collision myCol)
	{
		if(myCol.gameObject.tag == "Player")
		{
			myCol.transform.parent = transform;
			myCol.gameObject.GetComponent<playerScript>().updateScales();
		}
	}
	
	void OnCollisionExit(Collision myCol)
	{
		if(myCol.gameObject.tag == "Player")
		{
			myCol.transform.parent = null;
			myCol.gameObject.GetComponent<playerScript>().updateScales();
		}
	}
	*/
}
